package vashion.azeroth.core.service.impl;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.contant.CommentErrorEnum;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.experience.ExperienceValue;
import vashion.azeroth.core.manager.personal.PersonalCommentManager;
import vashion.azeroth.core.personal.pojo.PersonalAddress;
import vashion.azeroth.core.personal.pojo.PersonalComment;
import vashion.azeroth.core.personal.pojo.PersonalExperience;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalCommentService;
import vashion.azeroth.core.service.PersonalExperienceService;

@Service
public class PersonalCommentServiceImpl<T> implements PersonalCommentService<T> {
	private static final Logger logger = LoggerFactory.getLogger(PersonalCommentServiceImpl.class);
	
    private final static Logger log = LoggerFactory
            .getLogger(PersonalPublishFilesServiceImpl.class);
    
	@Autowired
	PersonalCommentManager personalCommentManager;

	@Autowired
	private PersonalExperienceService<T> personalExperienceService;
	
//	@Override
	public ApiResult<Integer> queryCommentCnt(Integer feedId) {

		if (null == feedId) {

			return ApiResult.newErrorResult(
					GenericErrorEnum.NOT_EMPTY.getErrorCode(), "帖子id"
							+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
		}

		try {

			int cnt = personalCommentManager.queryCommentCnt(feedId);

			return ApiResult.newSuccessResult(cnt);

		} catch (Exception e) {

			return ApiResult.newErrorResult(
					GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());

		}
	}

	public ApiResult<PersonalComment> addComment(PersonalComment personalComment) {
		ApiResult<PersonalComment> result = new ApiResult<PersonalComment>();

		try {
			personalComment.setCtime(new Date(System.currentTimeMillis()));
			personalComment.setTimeAdd(new Date(System.currentTimeMillis()));
			personalComment.setTimeEdit(new Date(System.currentTimeMillis()));
			logger.error("personalCommentManager.save,personalComment:{}",personalComment);
			PersonalComment addComment = personalCommentManager
					.save(personalComment);
			

	        if (null != addComment) {
	            
//	            发布评论成功后更新经验值
	            PersonalExperience experience = new PersonalExperience();
	            experience.setValueBase(ExperienceValue.COMMENT_UP.getCode());
				experience.setMemberId(addComment.getUserid());
				try {
					ApiResult<Integer> expRes = personalExperienceService.updateExperienceBase(experience);
					log.info("帖子发布者更新后经验值为： " + expRes.getData());
				} catch (Exception e) {
					log.error("更新帖子发布者经验值失败" + e);
				}
				
	        }
	        
			result = ApiResult.newSuccessResult(addComment);
		} catch (Exception e) {
			ApiResult.newErrorResult(
					CommentErrorEnum.ADD_COMM_FAIL.getErrorCode(),
					CommentErrorEnum.ADD_COMM_FAIL.getErrorMessage());
		}
		return result;
	}

}
